package cn.edu.zut.salarymanagesystem.activities;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;

import java.io.IOException;
import java.util.List;

import cn.edu.zut.salarymanagesystem.R;
import cn.edu.zut.salarymanagesystem.adapter.ItemAdapter;
import cn.edu.zut.salarymanagesystem.bean.pojo.Item;
import cn.edu.zut.salarymanagesystem.bean.vo.ListItemResult;
import cn.edu.zut.salarymanagesystem.config.Storage;
import cn.edu.zut.salarymanagesystem.utils.Browser;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ShowCanAddItemActivity extends AppCompatActivity {
    private static final int RECEIVE_DATA = 1;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case RECEIVE_DATA:
                    try {
                        ListView listView = ShowCanAddItemActivity.this.findViewById(R.id.lv_add_name);
                        String json = (String) msg.obj;
                        ListItemResult result = JSON.parseObject(json, ListItemResult.class);
                        List<Item> data = result.getList();
                        ItemAdapter itemAdapter = new ItemAdapter(data, ShowCanAddItemActivity.this);
                        listView.setAdapter(itemAdapter);
                        ShowCanAddItemActivity.this.list = data;
                        ShowCanAddItemActivity.this.adapter = itemAdapter;
                        itemAdapter.notifyDataSetChanged();
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.e("d8g", e.getMessage());
                    }

                    break;
            }
        }
    };
    private List<Item> list = null;
    private ItemAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_can_add_item);
        findViewById(R.id.btn_back).setOnClickListener(view -> ShowCanAddItemActivity.this.finish());
        OkHttpClient client = Browser.getClient();
        Request request = new Request.Builder().url(Storage.url + "/item/listMy").build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e("d8g", "获取工价数据出错！");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                Message obtain = Message.obtain();
                obtain.what = RECEIVE_DATA;
                obtain.obj = response.body().string();
                handler.sendMessage(obtain);
            }
        });
    }

}